<template>
  <div class="card-container">
    <el-card>
      <el-row type="flex" justify="space-between">
        <el-button size="small" type="primary" @click="dialogVisible = true">添加页面</el-button>
        <el-row type="flex">
          <el-input v-model="inputData" placeholder="请输入关键字" size="small" style="width:400px;" />
          <el-button size="small" @click="fuzzySearch">筛选</el-button>
        </el-row>
      </el-row>
      <el-divider />
      <el-table
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="title"
          label="标题"
          width="360"
        />
        <el-table-column prop="content" label="内容" width="360">
          <template v-slot="{row}">
            <div v-html="row.content" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button size="small" type="primary" @click="editPage(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delPage(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-divider />

      <!-- 分页组件 -->
      <el-row type="flex" align="middle" justify="center" style="height: 60px">
        <el-pagination
          :total="total"
          :page-size="page._limit"
          layout="prev, pager, next"
          @current-change="changePage"
        />
      </el-row>
    </el-card>

    <!-- 页面弹窗 -->
    <el-dialog
      :title="formData.id?'编辑页面':'添加页面'"
      :visible.sync="dialogVisible"
      width="75%"
      @close="closeFn"
    >
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item prop="title" label="标题">
          <el-input v-model="formData.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item prop="content" label="内容">
          <vue-editor v-model="formData.content" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getPageList, postPage, delPage, getPageById, putPage, getPageCount } from '@/api/pages'
import { VueEditor } from 'vue2-editor'
export default {
  components: { VueEditor },
  data () {
    return {
      total: 5, // 总条数
      inputData: '',
      tableData: [],
      page: {
        _limit: 5, // 每页长度
        _start: 0 // 开始获取的下标
      },
      formData: {
        title: '',
        content: ''
      },
      dialogVisible: false,
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getPageList()
  },
  methods: {
    // 模糊搜索
    async fuzzySearch() {
      if (this.inputData) {
        this.tableData = await getPageList({ title_contains: this.inputData })
        // console.log(res)
        this.total = await getPageCount({ title_contains: this.inputData })
      } else {
        this.getPageList()
      }
    },
    // 分页器分页事件
    changePage(page) {
      this.page._start = (page - 1) * this.page._limit
      this.getPageList()
    },
    // 获取页面列表
    async getPageList() {
      const res = await getPageList(this.page)
      // console.log(res)
      this.tableData = res
      // this.page.total = res.length
      // console.log(this.page)

      this.total = await getPageCount()
    },
    // 关闭弹窗
    closeFn() {
      this.formData = {
        title: '',
        content: ''
      }
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 弹窗确定按钮
    async confirm() {
      await this.$refs.form.validate()
      if (this.formData.id) {
        await putPage(this.formData)
      } else {
        await postPage(this.formData)
      }
      this.closeFn()
      this.getPageList()
    },
    // 删除页面
    delPage(id) {
      this.$confirm('确定删除吗', '删除', {
        type: 'warning'
      }).then(async _ => {
        await delPage(id)
        this.getPageList()
      }).catch(_ => {})
    },
    // 编辑页面
    async editPage(id) {
      this.dialogVisible = true
      const res = await getPageById(id)
      // console.log(res)
      this.formData = res
    }
  }
}
</script>
<style scoped>
.card-container{
  margin: 20px;
}
</style>
